from __future__ import annotations

from typing import Optional

from fastapi import APIRouter, Query

router = APIRouter()


@router.get("/healthz")
async def healthz() -> dict[str, bool]:
    return {"ok": True}


@router.get("/trading")
async def trading_health(run_id: Optional[str] = Query(None, description="运行ID")) -> dict:
    """交易系统健康检查（E2阶段）"""
    from ..services.health_check import check_health
    
    status = await check_health(run_id)
    
    return {
        "db_ok": status.db_ok,
        "mcp_ok": status.mcp_ok,
        "last_tick_time": status.last_tick_time.isoformat() if status.last_tick_time else None,
        "last_tick_delay_seconds": status.last_tick_delay_seconds,
        "consecutive_failures": status.consecutive_failures,
        "bad_args_count": status.bad_args_count,
        "overall_status": "healthy" if status.db_ok and status.mcp_ok and status.consecutive_failures < 3 else "unhealthy"
    }

